import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/user/req_user.dart';
import 'package:flutter_changdian_football/base/base_page_controller.dart';
import 'package:flutter_changdian_football/config/enum/refresh.dart';
import 'package:flutter_changdian_football/model/user/user_account_record_model.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/ext/refresher_extension.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class RechargeRecordController extends BaseGetPageController {
  
  List<UserAccountRecordModel> userAccountRecordModelList = [];

  @override
  void requestData(
    RefreshController controller, 
    {
      Refresh refresh = Refresh.first
    }
  ) async {
    var response = await ReqUser.userAccountRecord(
      pageNum: page,
      recordType: CustomConstant.RECHARGE
    );

    if (response.status == Status.completed) {
      if (response.data['code'] == '200') {
        /// 判断是否为最后一页, 如果是， over传true， 否则false
        if (response.data['data']['hasNextPage'] == false) {
          RefreshExtension.onSuccess(controller, refresh, true); 
        } else {
          RefreshExtension.onSuccess(controller, refresh, false);
        }
        RefreshExtension.onSuccess(controller, refresh, true); 

        /// 下拉刷新需要清除列表
        if (refresh != Refresh.down) {
          userAccountRecordModelList.clear();
          update();
        }

        for (int i = 0; i < response.data['data']['userAccountRecordVOList'].length; i++) {
          userAccountRecordModelList.add(UserAccountRecordModel.fromJson(response.data['data']['userAccountRecordVOList'][i]));
        }

        showSuccess(userAccountRecordModelList);
        update();
      } else if (response.data['code'] == 401) {
        RefreshExtension.onError(controller, refresh);
      } else {
        showError();  
        RefreshExtension.onError(controller, refresh);
      }
    } else {
      showError();  
      RefreshExtension.onError(controller, refresh);
    }
  }

}